﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class AuthorsBook : Form
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public List<int> selectedAuthors = new List<int>();
        public static ObjectSet<Author> authors;
        Book book;
        public AuthorsBook(Book book)
        {
            InitializeComponent();
            authors = ins.Author;
            this.book = book;
            if (book != null)
            {
                редактироватьToolStripMenuItem.Visible = false;
                удалитьToolStripMenuItem.Visible = false;
                bookView.RowHeadersVisible = true;
            }
         
        }

        private void AuthorsBook_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("idAuthor", "idAuthor");
            bookView.Columns["idAuthor"].Visible = false;

            bookView.Columns.Add("Surname", "Фамилия");
            bookView.Columns["Surname"].Width = 150;
            bookView.Columns["Surname"].ReadOnly = true;

            bookView.Columns.Add("Name", "Имя");
            bookView.Columns["Name"].Width = 150;
            bookView.Columns["Name"].ReadOnly = true;

            bookView.Columns.Add("Patronymic", "Отчество");
            bookView.Columns["Patronymic"].Width = 150;
            bookView.Columns["Patronymic"].ReadOnly = true;


            int i = 0;
            foreach (Author author in authors)
            {
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = author.idAuthor;
                bookView.Rows[i].Cells[1].Value = author.Surname;
                bookView.Rows[i].Cells[2].Value = author.Name;
                bookView.Rows[i].Cells[3].Value = author.Patronymic;
                i++;
            }
               
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ins = new LibraryEntities1();
            authors = ins.Author;

            if (bookView.CurrentRow != null)
            {

                int idAuthor = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                Author author = authors.Where(x => x.idAuthor.Equals(idAuthor)).FirstOrDefault();
                AuthorViewForm fm = new AuthorViewForm(author, false);
                fm.ShowDialog();
                bookView.CurrentRow.Cells[1].Value = fm.author.Surname;
                bookView.CurrentRow.Cells[2].Value = fm.author.Name;
                bookView.CurrentRow.Cells[3].Value = fm.author.Patronymic;


            }
            else
            {
                MessageBox.Show("Выберите запись для редактирования");
            }
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (book != null)
            {
                selectedAuthors = new List<int>();
                foreach (DataGridViewRow x in bookView.SelectedRows)
                {
                    selectedAuthors.Add(Convert.ToInt32(x.Cells[0].Value));
                }
                this.Close();
            }
            else
            {
                AuthorViewForm fm = new AuthorViewForm(null, true);
                fm.ShowDialog();
                if (fm.author != null)
                {
                    bookView.Rows.Add();

                    bookView.Rows[bookView.Rows.Count - 1].Cells[0].Value = fm.author.idAuthor;
                    bookView.Rows[bookView.Rows.Count - 1].Cells[1].Value = fm.author.Surname;
                    bookView.Rows[bookView.Rows.Count - 1].Cells[2].Value = fm.author.Name;
                    bookView.Rows[bookView.Rows.Count - 1].Cells[3].Value = fm.author.Patronymic;
                }
            }
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                int elementId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                if (authors.Where(x => x.idAuthor.Equals(elementId)).Count() == 1)
                {
                    Author element = authors.Where(x => x.idAuthor.Equals(elementId)).FirstOrDefault();
                    if (element.BookAuthor.Count > 0)
                        MessageBox.Show("Невозможно удаление. У автора есть книги");
                    else
                    {
                        authors.DeleteObject(element);
                        ins.SaveChanges();
                        bookView.Rows.Remove(bookView.CurrentRow);
                    }
                }
                else
                    bookView.Rows.Remove(bookView.CurrentRow);

            }
            else
            {
                MessageBox.Show("Выберите строку для удаления");
            }
        }

        private void книгиToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
